// HSConfigTree.cpp : implementation file
//
#include "stdafx.h"
#include "Borer.h"
#include "HSConfigTree.h"
#include "HsParaDefinition.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CHSConfigTree
CHSConfigTree::CHSConfigTree()
{
}

CHSConfigTree::~CHSConfigTree()
{
}


BEGIN_MESSAGE_MAP(CHSConfigTree, CEditTreeCtrl)
	//{{AFX_MSG_MAP(CHSConfigTree)
	ON_WM_CONTEXTMENU()
	ON_NOTIFY_REFLECT(NM_RCLICK, OnRclick)
	ON_WM_RBUTTONDOWN()
	ON_COMMAND(ID_ADD_CFGCHILD, OnAddCfgchild)
	ON_COMMAND(ID_ADD_CFGSIBLING, OnAddCfgsibling)
	ON_COMMAND(ID_ITP_CFGDELETE, OnItpCfgdelete)
	ON_WM_LBUTTONUP()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CHSConfigTree message handlers
void CHSConfigTree::OnRclick(NMHDR* pNMHDR, LRESULT* pResult) 
{
	//Work out the position of where the context menu should be
	CPoint p(GetCurrentMessage()->pt);
	CPoint pt(p);
	ScreenToClient(&pt);
	Select(HitTest(pt), TVGN_CARET);
	OnContextMenu(NULL, p);
	*pResult = 0;
}

void CHSConfigTree::OnContextMenu(CWnd* pWnd, CPoint point) 
{
	// TODO: Add your message handler code here
	CMenu menu;
	VERIFY(menu.LoadMenu(IDR_RIGHT_CFGTREE));
	CMenu* pPopup = menu.GetSubMenu(0);
	ASSERT(pPopup != NULL);
	pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y, this);

}

BOOL CHSConfigTree::InitializeImageList()
{
	HICON hIcon[4];
	m_imageET.Create(16,16, ILC_COLOR16 | ILC_MASK,8,8);//32*32

	hIcon[0] = (HICON) AfxGetApp()->LoadIcon(IDI_ICON1);
	hIcon[1] = (HICON) AfxGetApp()->LoadIcon(IDI_ICON2);
	hIcon[2] = (HICON) AfxGetApp()->LoadIcon(IDI_ICON3);
	hIcon[3] = (HICON) AfxGetApp()->LoadIcon(IDI_ICON4);

	for(int i = 0;i < 4;i++) {
		m_imageET.Add(hIcon[i]);
	}
	SetImageList(&m_imageET, TVSIL_NORMAL);
	return true;
}

HTREEITEM CHSConfigTree::InsertPie(LPCTSTR szIIAST, 
	HTREEITEM hParent /*= TVI_ROOT*/, HTREEITEM hInsertAfter /*= TVI_LAST*/)
{
	HTREEITEM htiSon = InsertItem(szIIAST, hParent, hInsertAfter);
	SetItemImage(htiSon, 2, 3);

	SetItemData(htiSon, (DWORD)htiSon);
	m_bsave =true;
	return htiSon;
}

void CHSConfigTree::DilatePie(HTREEITEM hDilated,void* dwDilate)
{
	if(dwDilate) {
      m_mapBulge.SetAt(hDilated,dwDilate);
	}
}

void CHSConfigTree::EffulgePie(HTREEITEM hDilated,void*& rValue)
{
    m_mapBulge.Lookup(hDilated,rValue);
}

void CHSConfigTree::CrimplePie(HTREEITEM hCP)
{
	if(hCP==TVI_ROOT) {
	  m_mapBulge.RemoveAll();
	}else {
	  m_mapBulge.RemoveKey(hCP);
	}
}

void CHSConfigTree::OnAddCfgchild() 
{
	// TODO: Add your command handler code here
      CWnd* pWndDialog = this;
      while (pWndDialog->GetStyle() & WS_CHILD)
	  {
		  pWndDialog = pWndDialog->GetParent();	
	  }
	  ((CHsParaDefinition*)pWndDialog)->OnMenuitemaddpara();
}

void CHSConfigTree::OnAddCfgsibling() 
{
	// TODO: Add your command handler code here
      CWnd* pWndDialog = this;
      while (pWndDialog->GetStyle() & WS_CHILD)
	  {
		  pWndDialog = pWndDialog->GetParent();	
	  }
	  ((CHsParaDefinition*)pWndDialog)->OnMenuiteminsert();
}

void CHSConfigTree::OnItpCfgdelete() 
{
	// TODO: Add your command handler code here
      CWnd* pWndDialog = this;
      while (pWndDialog->GetStyle() & WS_CHILD)
	  {
		  pWndDialog = pWndDialog->GetParent();	
	  }
	  ((CHsParaDefinition*)pWndDialog)->OnMenuitemdelpara();
}

void CHSConfigTree::OnLButtonUp(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	int iCount;

	if(m_bDragging)
	{
		m_bDragging = FALSE;

		if(m_bTimerABOVE_Active)
		{
			KillTimer(TVHT_ABOVE);
			m_bTimerABOVE_Active = false;
		}
		if(m_bTimerBELOW_Active)
		{
			KillTimer(TVHT_BELOW);
			m_bTimerBELOW_Active = false;
		}

		iCount = GetCount();
		if(!m_ttTransfer.AllocateTree(iCount))
		{
			AfxMessageBox("Out of memory");
			return;
		}

		// Dragging an item to itself or to a node of its subtree is
		// not allowed 
		if(((m_hCursor == m_ahCursor[CUR_COPY_ABOVE]) || 
			 (m_hCursor == m_ahCursor[CUR_MOVE_ABOVE])) &&
		    ((m_hitemDrag != m_hitemDrop)  &&
			 !ItemHasChildren(m_hitemDrag) &&
			 !ItemHasChildren(m_hitemDrop) &&
			 (GetLevelTree(m_hitemDrag) > 2) &&
			 (GetParentItem(m_hitemDrag) == GetParentItem(m_hitemDrop)))) 
		{
				SaveTreeData(m_ttTransfer, m_hitemDrag, OP_SUBTREE);
    			if(m_hCursor == m_ahCursor[CUR_MOVE_ABOVE])
				{
					DeleteItemM(m_hitemDrag);
				}
				OnLoadAbove(m_hitemDrop);

				CWnd* pWndDialog = this;
				while (pWndDialog->GetStyle() & WS_CHILD)
				{
					  pWndDialog = pWndDialog->GetParent();	
				}
				
				pWndDialog->PostMessage(MMM_UPDATE_SET,0,0);	
		}
		else
        {
			MessageBeep(0);
			SelectVisibleItem(m_hitemDrag);
        } 

		ReleaseCapture();
		SelectDropTarget(NULL);
   		m_hCursor = NULL;
        m_ttTransfer.DeAllocateTree();
	}

	CTreeCtrl::OnLButtonUp(nFlags, point);
}
